package com.stoken.service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stoken.dao.MemberRegisterInfoDao;
import com.stoken.domain.MemberRegisterInfo;
import com.stoken.service.MemberRegisterInfoService;
import com.stoken.web.control.vo.Message;
import com.symbio.dao.hibernate.GenericDaoImpl;
import com.symbio.service.impl.AbstractServiceImpl;

@Service
public class MemberRegisterInfoServiceImpl extends AbstractServiceImpl<MemberRegisterInfo, Long> implements MemberRegisterInfoService{

    @Autowired
	private MemberRegisterInfoDao memberRegisterInfoDao;

	@Override
	public GenericDaoImpl<MemberRegisterInfo, Long> getDao() {
		return memberRegisterInfoDao;
	}

	@Override
	public Message isLegalforRegister(String commendUsername,String parentUsername, int location) {
        
		/**
		 * 判断服務人員在不在銷售人員的網絡結構中
		 */
		String hql1 = "from MemberRegisterInfo where baseUsername = ?";
		MemberRegisterInfo reg = memberRegisterInfoDao.find(hql1 , new Object[]{commendUsername});
		if(reg == null){
			return new Message(0,"销售人员不存在");
		}
		String uLevel =reg.getRegisterLevel();
		
		String[] uLevels = uLevel.split("-");
		int[] uls = new int[]{new Integer(uLevels[0]),new Integer(uLevels[1])};
		MemberRegisterInfo register = memberRegisterInfoDao.find(hql1 , new Object[]{parentUsername});
		//判断服务人员是否存在
		if(register==null)
		{
			return new Message(0,"服务人员不存在或未激活");
		}
		MemberRegisterInfo newRegister = memberRegisterInfoDao.find("from MemberRegisterInfo where baseUsername = ? and registerAvtivestate = 1" , new Object[]{parentUsername});
		//判断服务人员是否存在
		if(newRegister==null)
		{
			return new Message(0,"服务人员不存在或未激活");
		}
		
		String rLevel = register.getRegisterLevel();
		String[] rLevels = rLevel.split("-");
		int[] rls = new int[]{new Integer(rLevels[0]),new Integer(rLevels[1])};
		
		//判断如何为<0，证明是不合法的服务人员
		int rowSpace = rls[0]-uls[0];
		int gradeSpace = rls[1]-uls[1];
		if(rowSpace < 0)
		{
			return new Message(0,"服务人员不在销售人员的网络结构中");
		}
		//如果在同一级就必须服务人员和销售人员为同一个人
		else if(rowSpace == 0 && gradeSpace != 0)
		{
			return new Message(0,"服务人员不在销售人员的网络结构中");
		}
		else if(rowSpace == 0 && gradeSpace == 0)
		{
		//如果为同一个人，则判断location的位置是否存在
		String hql = "from MemberRegisterInfo where parentUsername.baseUsername = ? and registerLocation = ?"; 
		Object[] values = new Object[]{parentUsername,location};
		MemberRegisterInfo registerLocation = memberRegisterInfoDao.find(hql, values);
		//如果不为null，表示被占用
		if(registerLocation != null)
		{
			    String tag = location ==0?"左区":"右区";
				return new Message(0,"服务人员"+parentUsername+"的"+tag+"已经被占用");
		}
		else
		{
			return new Message(1,"");
		}
		}
		else
		{
		//如果某行不在这个范围内，代表不在销售人员的网络结构中（这个地方很关键）
		int minG = (2<<(rowSpace-1))*(uls[1]-1)+1;
		int maxG = (2<<(rowSpace-1))*uls[1];
		
		if(rls[1]<minG || rls[1]>maxG)
		{
			return new Message(0,"服务人员不在销售人员的网络结构中");
		}	
		//判断location的位置是否存在
		String hql = "from MemberRegisterInfo where parentUsername.baseUsername = ? and registerLocation = ?"; 
		Object[] values = new Object[]{parentUsername,location};
		MemberRegisterInfo registerLocation = memberRegisterInfoDao.find(hql, values);
		//如果不为null，表示被占用
		if(registerLocation != null)
		{
			 String tag = location ==0?"左区":"右区";
			return new Message(0,"服务人员"+parentUsername+"的"+tag+"已经被占用");
		}
		}
		return new Message(1,"");
	}
	
	public static void main(String[] args) {
		String uLevel ="2-2";
		String[] uLevels = uLevel.split("-");
		int[] uls = new int[]{new Integer(uLevels[0]),new Integer(uLevels[1])};

		String rLevel = "5-9";
		String[] rLevels = rLevel.split("-");
		int[] rls = new int[]{new Integer(rLevels[0]),new Integer(rLevels[1])};
		
		//判断如何为<0，证明是不合法的服务人员
		int rowSpace = rls[0]-uls[0];
		int gradeSpace = rls[1]-uls[1];
		if(rowSpace < 0)
		{
			
		}
		//如果在同一级就必须服务人员和销售人员为同一个人
		if(rowSpace == 0 && gradeSpace != 0)
		{
		
		}
		else
		{
		
		}
		//
		int minG = (2<<(rowSpace-1))*(uls[1]-1)+1;
		int maxG = (2<<(rowSpace-1))*uls[1];
		
		
	
		if(rls[1]<minG || rls[1]>maxG)
		{
			
		}	
		
		
		
		
	}

	@Override
	public MemberRegisterInfo findRegisterByParentUserName(String hql,
			String parentUsername) {
		
		String hql1 = "from MemberRegisterInfo where baseUsername = ?";
		MemberRegisterInfo reg = memberRegisterInfoDao.find(hql1 , new Object[]{parentUsername});
		return reg;
	}

	@Override
	public MemberRegisterInfo findRegisterByUserName(String activeName) {
		return  memberRegisterInfoDao.find("baseUsername", activeName);
		
	}

	@Override
	public List<MemberRegisterInfo> findRegisterByCommendUsername(
			String commendUsername,int pageNo) {
		Session session = memberRegisterInfoDao.getSessionFactory().getCurrentSession();
		String hql = "from MemberRegisterInfo where registerCommendUsername.baseUsername = ? ";//order by baseFirstregisterTime desc
		Query query = session.createQuery(hql );
		query.setFirstResult(10*pageNo);
		query.setMaxResults(10);
		return query.list();
	}

	@Override
	public List<MemberRegisterInfo> findRegByPUsername(String pUsername) {
		String hql = "from MemberRegisterInfo where parentUsername.baseUsername = ? and registerAvtivestate = 1";
		List<MemberRegisterInfo> list = memberRegisterInfoDao.findList(hql, new Object[]{pUsername});
		return list;
	}

	@Override
	public Boolean isSystemAccount(String account) {
		String hql = "from MemberRegisterInfo where baseUsername = ? and registerAvtivestate = 1";
	    MemberRegisterInfo memberRegisterInfo = memberRegisterInfoDao.find(hql,new Object[]{account});
	    if(memberRegisterInfo != null){
	    	return true;
	    }
	    return false;
	}

}
